/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ejb;

import javax.ejb.EJB;
import javax.ejb.Stateless;
import javax.ejb.LocalBean;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;

/**
 *
 * @author Stefano
 */
@Stateless
@LocalBean
public class SponsoredEventManager {

    @EJB
    private SponsoredEventFacade sponsoredEventFacade;

    // Add business logic below. (Right-click in editor and choose
    // "Insert Code > Add Business Method")
    public void addSponsoredEvent(String userid, String eid, Date startdate, Date enddate, int maximpression, String name, String tags) {
        SponsoredEvent sevent = new SponsoredEvent();
        sevent.setUserID(userid);
        sevent.setEid(eid);
        sevent.setStartDate(startdate);
        sevent.setEndDate(enddate);
        sevent.setImpression(0);
        sevent.setMaxImpression(maximpression);
        sevent.setName(name);
        sevent.setTag(tags);
        //sevent.setGeo(geo);
        sponsoredEventFacade.create(sevent);
    }

    public SponsoredEvent findSposoredEventEid(String eid) {
        List<SponsoredEvent> sevent = sponsoredEventFacade.findAll();
        for (SponsoredEvent found : sevent) {
            if (found.getEid().equals(eid)) {
                return found;
            }
        }
        return null;
    }

    public int remainingViews(String eid) {
        SponsoredEvent se = new SponsoredEvent();
        se = findSposoredEventEid(eid);
        if (se != null) {
            return se.getMaxImpression() - se.getImpression();
        }
        return -1;
    }

    //restituisce la una lista di oggetti SposoredEvent di un determinato utente
    public List<SponsoredEvent> getEventUser(String uid) {
        List<SponsoredEvent> sevent = sponsoredEventFacade.findAll();
        List<SponsoredEvent> eventuser = new LinkedList<SponsoredEvent>();
        for (SponsoredEvent found : sevent) {
            if (found.getUserID().equals(uid)) {
                eventuser.add(found);
            }
        }
        return eventuser;
    }

    //restituisce la lista degli eid associati a un utente (userID)
    public List<String> findSposoredEventUid(String uid) {
        List<SponsoredEvent> sevent = sponsoredEventFacade.findAll();
        List<String> list = new LinkedList<String>();
        for (SponsoredEvent found : sevent) {
            if (found.getUserID().equals(uid)) {
                list.add(found.getEid());
            }
        }
        return list;
    }

    public boolean deleteSposnoredEventEid(String eid) {
        SponsoredEvent toDelete = new SponsoredEvent();
        toDelete = findSposoredEventEid(eid);
        if (toDelete != null) {
            sponsoredEventFacade.remove(toDelete);
            return true;
        }
        return false;
    }

    public void editSponsoredEvent(SponsoredEvent se) {
        sponsoredEventFacade.edit(se);
    }

    public String getAllSponsoredEvent() {
        String result = "";
        List<SponsoredEvent> sevent = sponsoredEventFacade.findAll();
        
        for (int i = 0, j=1; i < sevent.size(); i++, j++) {
            SponsoredEvent se = sevent.get(i);
            if (se.getImpression() < se.getMaxImpression()) {
                decrementSponsoredEvent(se);
                result = result + se.getEid();
                if (j < sevent.size()) {
                    result = result + ", ";
                }
            }
        }
        return result;
    }

    public boolean decrementSponsoredEvent(SponsoredEvent sevent) {
        sevent.setImpression(sevent.getImpression()+1);
        sponsoredEventFacade.edit(sevent);
        return true;
    }
    
    
    
}
